<?php 
namespace app\admin\controller;
use think\Db;
/**
 * 管理员 auth_group_name
 */
class Administrator extends Base{

	public function index(){
		$page = $this->request->param('page/d',1);
		$limit = $this->request->param('limit/d',20);
		$keyword = $this->request->param('keyword','','trim');
		$where=NULL;
		if( $keyword ){
			$where = $keyword ? " `username` LIKE '%".$keyword."%'" : '1=1';
		}
		$totalCount = Db::table('administrator')
				->alias('a')
				->field("a.id,a.username,a.`status`,a.isDel,a.create_time,ag.groupname")
				->where($where)
				->join(['auth_group'=>'ag'],'a.auth_group_id = ag.id')
				->count();
		$list = Db::table('administrator')
				->alias('a')
				->field("a.id,a.username,a.`status`,a.isDel,a.create_time,ag.groupname")
				->where($where)
				->join(['auth_group'=>'ag'],'a.auth_group_id = ag.id')
				->page($page,$limit)
				->order('id','ASC')
				->select();
		foreach ($list as $key => $value) {
			$list[$key]['create_time_str'] = date("Y-m-d H:i:s",$value['create_time']);
		}
		$data['code'] = 0;
		$data['data'] = $list;
		$data['count'] = $totalCount;
		$data['msg'] = '请求成功';

		if( $this->request->isAjax() ){
			return $data;
		}else{
			return $this->fetch();
		}
	}

	public function add(){
		if($this->request-> isPost() || $this->request-> isAjax()){
			$add['username'] = $this->request->param('username','');
			$add['password'] = encrypt($this->request->param('password',''));
			$add['auth_group_id'] = $this->request->param('auth_group_id','');
			$add['status'] = $this->request->param('status','');
			$add['isDel'] = $this->request->param('isDel','');
			$add['create_time'] = time();
			$res = db('administrator')->insert($add);
			if( $res != 1 ){
				addAdminLog('添加管理员，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('添加管理员，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			$auth_group = db('auth_group')->where('status = 0')->select();
			$this->assign('auth_group',$auth_group);
			return $this->fetch();
		}
	}

	public function edit(){
		$id = $this->request->param('id',0);
		if($this->request-> isPost() || $this->request-> isAjax()){
			$upd = input('post.');
			$res = db('administrator')->update($upd);
			if( $res != 1 ){
				addAdminLog('修改管理员信息，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('修改管理员信息，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			$auth_group = db('auth_group')->select();
			$info = db('administrator')->where('id = '.$id)->find();
			$this->assign('auth_group',$auth_group);
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	public function editPwd(){
		$id = $this->request->param('id',0);
		if($this->request-> isPost() || $this->request-> isAjax()){
			$upd['id'] = $id;
			$upd['password'] = encrypt($this->request->param('password',''));
			$res = db('administrator')->update($upd);
			if( $res != 1 ){
				addAdminLog('修改管理员密码，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('修改管理员密码，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			$this->assign('id',$id);
			return $this->fetch('editPwd');
		}
	}

	public function del(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$res = db('administrator')->where('id = '.$id)->delete();
			if( $res !== false ){
				addAdminLog('删除管理员，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('删除管理员，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$data= ['err'=>1,'msg'=>'操作失败！'];
			return $data;
		}
	}
	
}